; jQuery(function () {
	// 返回开发年份
	function getYears(developmentYear) {
		const date = new Date()
		const newYear = developmentYear !== date.getFullYear() ? developmentYear + ' - ' + date.getFullYear() : developmentYear
		return newYear
	}

	$('#developmentYear').text(getYears(2025))

	// 更新日志渲染
	const $updateDom = $('#update-dom')
	const $isNotice = $('#isNotice')

	$.ajax({
		type: 'GET',
		url: 'https://www.xfabe.com/nitaiv5pro/?logs=1024',
		success: function (response) {
			const { code, msg, data } = response

			if (code !== 200) {
				$updateDom.text(msg)
				return
			}

			const updateVersion = localStorage.getItem('updateVersion')
			const version = data[0].version

			$('#clcikNotice').on('click', function () {
				localStorage.setItem('updateVersion', version)
				$isNotice.css('opacity', '0')
			})
			
			if (updateVersion !== version || !updateVersion) {
				$isNotice.css('opacity', '1')
			}

			$updateDom.html(data.map(item => `
				<div class="card mb-4 shadow-sm border-light">
					<div class="card-header bg-primary text-white">
						<h5 class="card-title">${item.title}</h5>
						<p class="card-text">Version: <strong>${item.version}</strong></p>
					</div>
					<ul class="list-group list-group-flush">
						${item.data.map(entry => `
							<li class="list-group-item d-flex justify-content-between align-items-center">
								<span>${entry}</span>
							</li>
						`).join('')}
					</ul>
				</div>
			`).join(''))
		}
	})

	$('#clearCache').on('click', function () {
		swal({
				title: '确定要清除前台所有缓存吗?',
				text: '清除后将不可恢复',
				icon: 'warning',
				buttons: ['取消', '清除']
			})
			.then(result => {
				if (result) {
					$.ajax({
						type: 'GET',
						url: '/api/config/clearAllCache',
						success: function (response) {
							const { code, msg } = response

							if (code === 404) {
								return swal('暂无缓存', '', 'info')
							}

							switch (code) {
								case 200:
									swal(msg, '', 'success')
									break;
								case 400:
									swal(msg, '', 'info')
									break;
								default:
									swal(msg, '', 'error')
									break;
							}
						}
					})
				}
			})
	})

	$('#logInOut').on('click', function () {
		swal({
			title: '确定要退出登录吗?',
			text: '',
			icon: 'warning',
			buttons: ['取消', '退出']
		})
			.then(result => {
				if (result) {
					const currentUrl = window.location.href
					const { search } = new URL(currentUrl)
					
					$.ajax({
						type: 'GET',
						url: '/api/users/logout',
						success: function (response) {
							const { code, msg } = response

							swal(msg, '', code === 200 ? 'success' : 'error')
								.then(() => {
									window.location.href = search ? '/admin/login.html' + search : '/admin/login.html'
								})
						}
					})
				}
			})
	})

	$('#restartProject').on('click', function () {
		swal({
			title: '确定要重启此项目么?',
			text: '',
			icon: 'warning',
			buttons: ['取消', '确定']
		})
			.then(result => {
				if (result) {
					$.ajax({
						type: 'GET',
						url: '/api/config/restartServer',
						success: function (response) {
							const { code, msg } = response

							if (code !== 200 || msg.status !== 'processing') {
								swal(msg, '', 'error')
								return
							}

							const loading = $('body').lyearloading({
								opacity: 0.5,
								spinnerSize: 'lg',
								spinnerText: '项目重启中，请稍后...',
								textColorClass: 'text-info',
								spinnerColorClass: 'text-info'
							})

							let timer = setInterval(async () => {
								try {
									const fetchOptions = {
										method: 'POST',
										headers: {
											'Content-Type': 'application/json'
										}
									}
									const response = await fetch('/api/config/home', fetchOptions)
									const result = await response.json()
									if (result.code !== 200) {
										return
									}
									
									clearInterval(timer)
									timer = null
									loading.destroy()
									swal('重启成功', '', 'success')
										.then(() => location.reload())
								} catch (error) {
									return
								}
							}, 500)
						}
					})
				}
			})
	})

	$('#closedProject').on('click', function () {
		swal({
			title: '确定要关闭此项目么?',
			text: '',
			icon: 'warning',
			buttons: ['取消', '确定']
		})
			.then(result => {
				if (result) {
					$.ajax({
						type: 'GET',
						url: '/api/config/closeServer',
						success: function (response) {
							if (response.code !== 200) {
								swal(response.msg, '', 'error')
								return
							}

							const loading = $('body').lyearloading({
								opacity: 0.5,
								spinnerSize: 'lg',
								spinnerText: '项目关闭中，请稍后...',
								textColorClass: 'text-info',
								spinnerColorClass: 'text-info'
							})

							setTimeout(async () => {
								loading.destroy()
								swal('关闭成功', '', 'success')
									.then(() => location.reload())
							}, 1500)
						}
					})
				}
			})
	})
})